//
//  DoorTeachViewController.m
//  MM
//
//  Created by 郑渊文 on 5/29/15.
//  Copyright (c) 2015 王晨辉. All rights reserved.
//

#import "DoorTeachViewController.h"
#import "CommonMacro.h"
#import "MySideView.h"
#import "LessionDetailViewController.h"
#import "PayViewController.h"
#import "TechingViewController.h"
#import "StoreViewController.h"
#import "SummaryViewController.h"
#import "RegistrationViewController.h"

@interface DoorTeachViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIBarButtonItem *rightBtn;
    BOOL ifshowSide;
    
    
    UIButton *backBtn;
    
}

@property(nonatomic,copy)NSMutableArray *nameArray;
@property(nonatomic,copy)MySideView *sideView;
@property(nonatomic,copy)NSMutableArray *typeArray;
@property(nonatomic,copy)NSMutableArray *chooseArray;
@property(nonatomic,copy)NSMutableArray *classStatusArray;
@property(nonatomic,copy)NSString *typeName;
@property(nonatomic,copy)NSString *statusId;
@property(nonatomic,copy)NSMutableArray *classIdArray;
@property(nonatomic,copy)NSMutableArray *jionIdArray;
@property(nonatomic,copy)NSMutableArray *priceArray;
@property(nonatomic,copy)UITableView *myTable;
@property(nonatomic,copy)UIView *grayView;
@property(nonatomic,copy)UIButton *RegistBtn;

@end

@implementation DoorTeachViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubViews];
    _nameArray = [NSMutableArray array];
    _typeArray = [NSMutableArray array];
    _classStatusArray = [NSMutableArray array];
     _classIdArray = [NSMutableArray array];
    _priceArray = [NSMutableArray array];
    _jionIdArray = [NSMutableArray array];
    
    
    NSDictionary *param4 = @{@"babyId":[BabyManager shareInstance].babyId,@"classStatus":_statusId==nil?@"4":_statusId};
    
    [UserDAL getTeachstatusParams:param4 withBlock:^(NSDictionary *data, NSError *error) {
        if (data) {
            ////NSLog(@"what%@",data);
            NSArray *array = data[@"dataList"];
            if (array.count!=0) {
                for (NSDictionary *areaDic in array) {
                    [_nameArray addObject:areaDic[@"className"]] ;
                    [_typeArray addObject:areaDic[@"typeName"]] ;
                    [_classStatusArray addObject:areaDic[@"classStatus"]] ;
                    [_classIdArray addObject:areaDic[@"classId"]] ;
                    [_priceArray addObject:areaDic[@"classPrice"]] ;
                    [_jionIdArray addObject:areaDic[@"joinId"]] ;
                }
                ////NSLog(@"===222222222==========%@",_classIdArray);
                [_myTable reloadData];
            }
            else
            {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"对不起，你所在的地区没有麦苗公学。" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                
                
                [alert show];
            }
            
        }else
        {
            
        }
    }];

    _grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _grayView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_grayView];
    _grayView.alpha = 0.5;
    _grayView.hidden = YES;
    
    //    _nameArray = [NSMutableArray arrayWithObjects:@"《文语文语文》",@"《文语文语文》",@"《文语文语文》",@"《文语文语文》", nil];
    //    _typeArray = [NSMutableArray arrayWithObjects:@"等待支付",@"等待支付",@"上课中",@"已结束", nil];
    _chooseArray = [NSMutableArray arrayWithObjects:@"全部",@"待支付",@"上课中",@"已结束",@"等待上课", nil];
    
    self.view.backgroundColor = [UIColor yellowColor];
    ifshowSide = YES;
    _sideView = [[MySideView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2,0, SCREEN_WIDTH/2, SCREEN_HEIGHT)];
    _sideView.dataSources = _chooseArray;
    
    _sideView.myifshowBlock = ^(BOOL ifshow){
        ifshowSide = ifshow;
    };
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)turntoChoose:(UIButton *)mySwitch

{
    mySwitch.selected = ifshowSide;
    ifshowSide = !mySwitch.selected;
    _grayView.hidden = !mySwitch.selected;
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.2];
    [animation setType:kCATransitionPush];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [self.view addSubview:_sideView];
    
    if (ifshowSide==NO) {
        [animation setSubtype:kCATransitionFromRight];
        _sideView.hidden = NO;
        [[_sideView layer] addAnimation:animation forKey:@"switch"];
    }
    if (ifshowSide==YES) {
        [animation setSubtype:kCATransitionFromLeft];
        _sideView.hidden = YES;
        [[_sideView layer] addAnimation:animation forKey:@"switch"];
    }
    __weak DoorTeachViewController *weakSelf = self;
    _sideView.mysideblock = ^(NSString *myStr){
        _typeName = myStr;
        if ([weakSelf.typeName isEqualToString:@"待支付"]) {
            _statusId = @"0";
        }else if([weakSelf.typeName isEqualToString:@"等待上课"])
        {
            weakSelf.statusId = @"1";
        }
        else if([weakSelf.typeName isEqualToString:@"上课中"])
        {
            weakSelf.statusId = @"2";
        }
        else if([weakSelf.typeName isEqualToString:@"已结束"])
        {
            weakSelf.statusId = @"3";
        }else
        {
            weakSelf.statusId = @"4";
        }
        
        NSDictionary *param4 = @{@"babyId":[BabyManager shareInstance].babyId,@"classStatus":weakSelf.statusId==nil?@"4":weakSelf.statusId};
        ////NSLog(@"zhuangtai s%@",weakSelf.statusId);
        [UserDAL getTeachstatusParams:param4 withBlock:^(NSDictionary *data, NSError *error) {
            if (data) {
                ////NSLog(@"what%@",data);
                NSArray *array = data[@"dataList"];
                [weakSelf.nameArray removeAllObjects];
                [weakSelf.typeArray removeAllObjects];
                [weakSelf.classStatusArray removeAllObjects];
                [weakSelf.classIdArray removeAllObjects];
                for (NSDictionary *areaDic in array) {
                    
                    [weakSelf.nameArray addObject:areaDic[@"className"]] ;
                    [weakSelf.typeArray addObject:areaDic[@"typeName"]] ;
                    [weakSelf.classStatusArray addObject:areaDic[@"classStatus"]] ;
                    [weakSelf.classIdArray addObject:areaDic[@"classId"]] ;
                    [weakSelf.priceArray addObject:areaDic[@"classPrice"]] ;
                    [weakSelf.jionIdArray addObject:areaDic[@"joinId"]] ;
                }
                ////NSLog(@"数组里得数据位%@",weakSelf.nameArray);
                [weakSelf.myTable reloadData];
                
            }else
            {
                

                
                
            }
        }];
        
        weakSelf.grayView.hidden = YES;
        [weakSelf.myTable reloadData];
        ////NSLog(@"myType是什么%@",weakSelf.typeName);
        [weakSelf.RegistBtn setTitle:[NSString stringWithFormat:@"%@",weakSelf.typeName] forState:UIControlStateNormal];
    };
}

-(void)initSubViews
{
    //自定义左右按钮
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"_0000_返回.png"]
                      forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back)
     forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 20, 15);
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = menuButton;
    
    self.navigationItem.title = @"上门早教";
    [self.navigationController.navigationBar setTranslucent:NO];//设置navigationbar的半透明
    [self.navigationController.navigationBar setBarTintColor:[GetFileModel colorWithHexString:@"#34bd81" alpha:1]];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    self.view.backgroundColor = [UIColor clearColor];
    
    
    _RegistBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-80, 7,80, 30)];
    [_RegistBtn setTitle:@"全部" forState:UIControlStateNormal];
    [_RegistBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_RegistBtn addTarget:self action:@selector(turntoChoose:) forControlEvents:UIControlEventTouchUpInside];
//    _RegistBtn.backgroundColor = UIColorWithRGBA(219, 99, 101, 1);
    [self.navigationController.navigationBar addSubview:_RegistBtn];
    
    
    _myTable = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _myTable.delegate = self;
    _myTable.dataSource = self;
    [self.view addSubview:_myTable];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _nameArray.count;
}


#pragma mark--- tableViewDelegate 区尾的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 1;
    
}
#pragma mark--- tableViewDelegate cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *NOTIFY = @"cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:NOTIFY];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NOTIFY];
    }else{
        for (UIView *subView in cell.subviews)
        {
            [subView removeFromSuperview];
        }
    }
    
    
    UILabel *classLable = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 80, 30)];
    [cell addSubview:classLable];
    classLable.text = _nameArray[indexPath.row];
    classLable.font = [UIFont systemFontOfSize:15];
    classLable.adjustsFontSizeToFitWidth = YES;
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    UILabel *typeLable = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-150, 10, 80, 30)];
    [cell addSubview:typeLable];
    typeLable.text = _typeArray[indexPath.row];
    typeLable.font = [UIFont systemFontOfSize:12];
    if ([typeLable.text isEqualToString:@"等待上课"]) {
        typeLable.textColor = [UIColor blueColor];
    }else if([typeLable.text isEqualToString:@"待支付"]) {
        typeLable.textColor = [UIColor orangeColor];
    }else if([typeLable.text isEqualToString:@"上课中"]) {
        typeLable.textColor = [UIColor greenColor];
    }else
    {
        typeLable.textColor = [UIColor lightGrayColor];
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([_typeArray[indexPath.row] isEqualToString:@"等待上课"]) {
        LessionDetailViewController *sugVC = [[LessionDetailViewController alloc]init];
        sugVC.classId = _classIdArray[indexPath.row];
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:sugVC];
        [self  presentViewController:nav animated:YES completion:nil];
//        [self.navigationController pushViewController:sugVC animated:NO];
    }else if([_typeArray[indexPath.row] isEqualToString:@"待支付"])
    {
        RegistrationViewController *sugVC = [[RegistrationViewController alloc]init];
        sugVC.classId = _classIdArray[indexPath.row];
        sugVC.priceStr = _priceArray[indexPath.row];
        sugVC.jionId = _jionIdArray[indexPath.row];
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:sugVC];
        [self  presentViewController:nav animated:YES completion:nil];
        
    }else if([_typeArray[indexPath.row] isEqualToString:@"上课中"])
    {
        LessionDetailViewController *sugVC = [[LessionDetailViewController alloc]init];
        sugVC.classId = _classIdArray[indexPath.row];
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:sugVC];
        [self  presentViewController:nav animated:YES completion:nil];
    }else
    {
        SummaryViewController *setVC = [[SummaryViewController alloc]init];
        setVC.classId = _classIdArray [indexPath.row];
        ////NSLog(@"%@=============",setVC.classId);
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:setVC];
        [self  presentViewController:nav animated:YES completion:nil];
    }
    
}

#pragma mark - HUD
- (void)ShowProgressHUDwithMessage:(NSString *)msg
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = msg;
    hud.dimBackground = NO;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:1];
}

-(void)back
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    
    
    switch (buttonIndex) {
        case 0:
        {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
            break;
            
        default:
            break;
    }
}

@end
